package com.sundablog.controller.systemsetup.logSetting;


import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
       
import com.sundablog.controller.systemsetup.logSetting.RequestUtil;
import com.sundablog.pojo.AdminLogWithBLOBs;
import com.sundablog.pojo.AdminUser;
import com.sundablog.service.systemsetup.logSetting.LogService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * 日志记录AOP实现
 * @ClassName:  LogAspect   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: 哒哒 
 * @date:   2018年2月28日 下午6:59:34   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Aspect
public class LogAspect {

	private static final Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);

	// 开始时间
	private long startTime = 0L;
	// 结束时间
	private long endTime = 0L;

	@Autowired
	private LogService logService;

	@Before("execution(* *..controller..*.*(..))")
	public void doBeforeInServiceLayer(JoinPoint joinPoint) {
		startTime = System.currentTimeMillis();
	}

	@After("execution(* *..controller..*.*(..))")
	public void doAfterInServiceLayer(JoinPoint joinPoint) {
	}

	@Around("execution(* *..controller..*.*(..))")
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		// 获取request
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
		HttpServletRequest request = servletRequestAttributes.getRequest();
		Object result = pjp.proceed();
		//获取cpu 和内存不加入log日志
		if ("/Memory".equals(request.getRequestURI())) {
			return result;
		}
		if ("/Cpu".equals(request.getRequestURI())) {
			return result;
		}
		if (null == request.getUserPrincipal()) {
			return result;
		}
		
		
		AdminLogWithBLOBs adminLogWithBLOBs = new AdminLogWithBLOBs();

		
		Signature signature = pjp.getSignature();
		MethodSignature methodSignature = (MethodSignature) signature;
		Method method = methodSignature.getMethod();
		
		endTime = System.currentTimeMillis();
		adminLogWithBLOBs.setBasePath(RequestUtil.getBasePath(request));
		adminLogWithBLOBs.setIp(RequestUtil.getIpAddr(request));
		adminLogWithBLOBs.setMethod(request.getMethod());
		if ("GET".equalsIgnoreCase(request.getMethod())) {
			adminLogWithBLOBs.setParameter(request.getQueryString());
		} else {
			adminLogWithBLOBs.setParameter(request.getParameterMap().toString());
		}
		if (null != result) {
			adminLogWithBLOBs.setResult(JSONUtil.toJsonStr(result));
		} else {
			adminLogWithBLOBs.setResult("");
		}
		adminLogWithBLOBs.setSpendTime((int) (endTime - startTime));
		adminLogWithBLOBs.setStartTime(DateUtil.date(startTime));
		adminLogWithBLOBs.setUri(request.getRequestURI());
		adminLogWithBLOBs.setUrl(request.getRequestURL().toString());
		adminLogWithBLOBs.setUserAgent(request.getHeader("User-Agent"));
		if (request.getUserPrincipal() != null) {
			AdminUser adminUser = (AdminUser)SecurityUtils.getSubject().getPrincipal();
			System.err.println(adminUser.getUsername());
		} else {
			adminLogWithBLOBs.setUsername("");
		}
		logService.addAdminLog(adminLogWithBLOBs);
		return result;
	}

}
